package cn.vcjf.weapp.common.service;

import cn.vcjf.weapp.common.exception.ServiceException;
import cn.vcjf.weapp.common.util.QiniuUploadFile;
import com.qiniu.util.Base64;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Auther: zhengzihang
 * @Date: 2018/9/25 11:47
 * @Description:
 */
public class FileUploadServiceImpl extends FileUploadService{

	@Override
	public String uploadObject(String title,
	                           MultipartFile mFile) {
		String fileId = "";
		System.out.println("title.isEmpty()="+title.isEmpty());
		//1.实现文件上传
		//1.1验证参数有效性
		if(title==null||title.trim().length()==0)
			throw new ServiceException("上传标题不能为空");
		if(mFile==null)
			throw new ServiceException("需要选择上传文件");
		if(mFile.isEmpty())
			throw new ServiceException("上传文件不能为空");

		//获得文件中的字节(应在文件上传之前)
		byte buf[]=null;
		try{
			buf=mFile.getBytes();
			String file64 = Base64.encodeToString(buf, 0);
			fileId = QiniuUploadFile.put64(file64);


			System.out.println("file的Id = "+fileId);
		}catch(Exception e){
			e.printStackTrace();
			throw new ServiceException("文件摘要创建失败");
		}

		if(StringUtils.isBlank(fileId))
			throw new ServiceException("insert error");
		return fileId;
	}
}
